package com.jae.controller.role;


import com.jae.controller.BaseController;
import com.jae.model.ResultMsg;
import com.jae.model.ServiceRuntimeException;
import com.jae.model.role.Role;
import com.jae.model.user.User;
import com.jae.service.RoleService;
import com.jae.utils.DateUtil;
import com.jae.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 角色Controller
 * 
 * @author Jae
 * @email 1571878499@qq.com
 * @date 2020-12-01 09:22:10
 */
@RestController
@RequestMapping("/admin/role")
public class RoleAdminController extends BaseController {

	@Autowired
	private RoleService roleService;

	/**
	 * /admin/Role/all 所有角色列表
	 */
	@RequestMapping(value = "/all", method = RequestMethod.POST)
	public ResultMsg all(HttpServletRequest request, String role_id, String role_name, String created_time) {
		String interfaceName = "所有角色列表";
		try {
			Map map = new HashMap();
			if (StringUtil.hasTest(role_id))
				map.put("role_id", role_id.trim());
			if (StringUtil.hasTest(role_name))
				map.put("role_name", role_name.trim());
			DateUtil.handleDaterangpickerData(created_time, "created_time", map);
			return new ResultMsg(true, roleService.findAll(map));
		} catch (ServiceRuntimeException e) {
			return fail(e);
		} catch (Exception e) {
			return error(interfaceName, e, request);
		}
	}

	/**
	 * /admin/role/info 获取指定角色信息
	 */
	@RequestMapping(value = "/info")
	public ResultMsg info(HttpServletRequest request, Integer role_id) {
		String interfaceName = "获取指定角色信息";
		try {
			return new ResultMsg(true, roleService.findById(role_id));
		} catch (ServiceRuntimeException e) {
			return fail(e);
		} catch (Exception e) {
			return error(interfaceName, e, request);
		}
	}

	/**
	 * /admin/role/add 添加角色
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public ResultMsg add(HttpServletRequest request, Role role) {
		String interfaceName = "添加角色";
		try {
			User user = getUser();
			return new ResultMsg(true, roleService.insert(role, user));
		} catch (ServiceRuntimeException e) {
			return fail(e);
		} catch (Exception e) {
			return error(interfaceName, e, request);
		}
	}

	/**
	 * /admin/role/update 修改角色
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public ResultMsg update(HttpServletRequest request, Role role) {
		String interfaceName = "修改角色";
		try {
			User user = getUser();
			if(roleService.update(role, user))
				return new ResultMsg(true);
			return new ResultMsg(false, "修改角色失败");
		} catch (ServiceRuntimeException e) {
			return fail(e);
		} catch (Exception e) {
			return error(interfaceName, e, request);
		}
	}

	/**
	 * /admin/role/delete 物理删除角色
	 */
	@RequestMapping(value = "/delete")
	public ResultMsg delete(HttpServletRequest request, Integer role_id) {
		String interfaceName = "物理删除角色";
		try {
			if (roleService.delete(role_id))
				return new ResultMsg(true);
			return new ResultMsg(false, "失败");
		} catch (ServiceRuntimeException e) {
			return fail(e);
		} catch (Exception e) {
			return error(interfaceName, e, request);
		}
	}

}
